package com.aistar.controller;

import com.aistar.model.bo.EmployerResult;
import com.aistar.model.bo.Result;
import com.aistar.model.dto.employer.BlacklistWithEmployeeNameDTO;
import com.aistar.model.dto.employer.EmployerAndUserDTO;
import com.aistar.model.dto.employer.InterviewDTO;
import com.aistar.model.dto.employer.ResumeAndDeliveryDTO;
import com.aistar.model.entity.Employer;
import com.aistar.model.entity.Interview;
import com.aistar.model.entity.Recruitment;
import com.aistar.model.entity.Resume;
import com.aistar.service.employer.IEmployerCenterService;
import com.aistar.service.employer.IEmployerRecruitmentService;
import com.aistar.service.employer.IEmployerResumeService;
import com.aistar.service.employer.IInterviewService;
import com.aistar.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: EmployerRestController
 * @author: LiuWenXiu
 * @date: 2021/9/26 15:46
 * @Description: TODO...本类用来演示:招聘者控制层
 */
@RestController
@RequestMapping("/employer")
public class EmployerRestController {

    @Autowired
    private IEmployerCenterService employerCenterService;

    @Autowired
    private IEmployerResumeService employerResumeService;

    @Autowired
    private IEmployerRecruitmentService employerRecruitmentService;

    @Autowired
    private IInterviewService interviewService;

    // 获取我的黑名单
    @GetMapping("/blacklist/data")
    public Result blacklist(HttpServletRequest request) {
        HttpSession session = request.getSession();
        // 加载招聘者信息
        Employer employer = (Employer) session.getAttribute("employer");
        Integer employerId = employer.getId();
        // 根据用户id查询用户的黑名单信息
        List<BlacklistWithEmployeeNameDTO> blacklists = employerCenterService.loadBlacklist(employerId);
//        System.out.println(blacklists);
        // 返回数据
        return new Result("200", "查询成功", blacklists);
    }

    // 添加黑名单
    @PostMapping("/add/blacklist")
    public Result addBlacklist(HttpServletRequest request, Integer resumeId) {
        HttpSession session = request.getSession();
        // 加载招聘者信息
        Employer employer = (Employer) session.getAttribute("employer");
        Integer employerId = employer.getId();
        int count = employerCenterService.pullBlack(resumeId, employerId);
        if (count == -1) {
            return new Result("501", "很抱歉,您的屏蔽列表已有该用户.");
        } else if (count == 0) {
            return new Result("500", "很抱歉,请重试");
        } else {
            return new Result("200", "屏蔽成功");
        }
    }

    // 删除选中的黑名单
    @PostMapping("/del/blacklist")
    public Result delBlacklist(Integer id) {
//        System.out.println(id);
        int count = employerCenterService.removeBlacklist(id);
        if (count != 0) {
            return new Result("200", "删除成功");
        }
        return new Result("500", "删除失败");
    }

    // 删除选中的职位
    @PostMapping("/del/recruitment")
    public Result delRecruitment(Integer id) {
        int count = employerRecruitmentService.delRecruitmentById(id);
        if (count != 0) {
            return new Result("200", "删除成功");
        }
        return new Result("500", "删除失败");
    }

    // 批量删除多个职位
    // ids是前端传过来的字符串 例如25,26,28
    @PostMapping("/del/recruitment/batch")
    public Result delRecruitmentBatch(String ids) {
        // 批量删除
        int count = employerRecruitmentService.delRecruitmentBatchById(ids);
        if (count != 0) {
            // 返回受影响行数
            return new Result("200", "删除成功", count);
        } else {
            return new Result("500", "删除失败");
        }
    }

    // 批量删除多个投递记录
    @PostMapping("/del/delivery/batch")
    public Result delDeliveryBatch(String ids) {
        // 批量删除
        int count = employerResumeService.delRecruitmentBatchById(ids);
        if (count != 0)
            return new Result("200", "删除成功", count);
        return new Result("500", "删除失败");
    }

    // 获取简历中心
//    @GetMapping("/resume/center/data")
//    public Result resumeCenter(){
////        List<Resume> resumeList = employerResumeService.loadResumeCenter();
////        if(null==resumeList){
////            return new Result("500","查询失败");
////        }
////        return new Result("200","查询成功",resumeList);
//        return null;
//    }

    // 获取简历中心 - 分页数据
    @GetMapping("/resume/center/page")
    public EmployerResult resumeCenterPage(HttpServletRequest request, @RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "5") int limit) {
        HttpSession session = request.getSession();
        // 加载招聘者信息
        Employer employer = (Employer) session.getAttribute("employer");
        Integer employerId = employer.getId();
        // 查询所有的数据
        List<Resume> resumeAll = employerResumeService.loadResumeCenter(employerId);
        // 分页查询的数据
        List<Resume> resumes = employerResumeService.loadResumeCenterPage(employerId, page, limit);
        if (null == resumes || resumes.size() == 0)
            return new EmployerResult("500", "查询失败");
        return new EmployerResult("200", "查询成功", resumes, resumeAll.size());
    }


    @GetMapping("/recruitment/post/page")
    public EmployerResult recruitmentPost(HttpServletRequest request, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit) {

        // 查询该招聘者所在公司的所有发布的职位
        // 获取招聘者的id-> 假设是10
        HttpSession session = request.getSession();
        // 加载招聘者信息
        Employer employer = (Employer) session.getAttribute("employer");
        Integer employerId = employer.getId();
        List<Recruitment> recruitmentList = employerRecruitmentService.loadMyPostRecruitmentByPage(employerId, page, limit);
        // 查询所有的数据
        List<Recruitment> recruitmentListAll = employerRecruitmentService.loadMyPostRecruitment(employerId);
        return new EmployerResult("200", "查询成功", recruitmentList, recruitmentListAll.size());
    }

    @PostMapping("/recruitment/refresh")
    public Result recruitmentRefresh(Integer id) {
        int count = employerRecruitmentService.updateReleaseTimeById(id);
        if (count != 0) {
            return new Result("200", "刷新成功");
        }
        return new Result("404", "刷新成功");
    }

    // 更新职位
    @PostMapping("/recruitment/update")
    public Result recruitmentUpdate(HttpServletRequest request, Recruitment recruitment, String tags) {
        // 创建职位之前还需要先获取一下招聘者id - 还没做所以先写死
        HttpSession session = request.getSession();
        // 加载招聘者信息
        Employer employer = (Employer) session.getAttribute("employer");
        Integer employerId = employer.getId();
        // 进行校验
        if (recruitment.getName() == null || recruitment.getName().trim().length() == 0) {
            return new Result("500", "职位名称不能为空");
        }
        if(recruitment.getSalaryUpper()==null || recruitment.getSalaryLower()==null){
            return new Result("501","薪资范围不能为空");
        }
        if (recruitment.getContactName() == null || recruitment.getContactName().trim().length() == 0) {
            return new Result("502", "联系人不能为空");
        }
        if (recruitment.getContactPhone() == null || recruitment.getContactPhone().trim().length() == 0) {
            return new Result("503", "联系电话不能为空");
        }
        if (recruitment.getEmail() == null || recruitment.getEmail().trim().length() == 0) {
            return new Result("504", "联系邮箱不能为空");
        }
        int count = employerRecruitmentService.updateRecruitmentById(recruitment, tags);
        if (count != 0) {
            return new Result("200", "修改成功", recruitment.getId());
        }
        return new Result("400", "修改失败");
    }


    // 创建新的职位
    @PostMapping("/recruitment/create")
    public Result recruitmentCreate(HttpServletRequest request, Recruitment recruitment, String tags) {
        System.out.println(recruitment);

        // 创建职位之前还需要先获取一下招聘者id - 还没做所以先写死
        HttpSession session = request.getSession();
        // 加载招聘者信息
        Employer employer = (Employer) session.getAttribute("employer");
        Integer employerId = employer.getId();

//        System.out.println(recruitment);
//        System.out.println(tags);
        // 进行校验
        if (recruitment.getName() == null || recruitment.getName().trim().length() == 0) {
            return new Result("500", "职位名称不能为空");
        }
        if(recruitment.getSalaryUpper()==null || recruitment.getSalaryLower()==null){
            return new Result("501","薪资范围不能为空");
        }
        if (recruitment.getContactName() == null || recruitment.getContactName().trim().length() == 0) {
            return new Result("502", "联系人不能为空");
        }
        if (recruitment.getContactPhone() == null || recruitment.getContactPhone().trim().length() == 0) {
            return new Result("503", "联系电话不能为空");
        }
        if (recruitment.getEmail() == null || recruitment.getEmail().trim().length() == 0) {
            return new Result("504", "联系邮箱不能为空");
        }
        // 走到这一步说明已经通过了校验
        int recruitmentId = employerRecruitmentService.createNewRecruitment(employerId, recruitment, tags);
        if (recruitmentId == 0)
            return new Result("505", "很抱歉,请您重试");
        return new Result("200", "发布成功", recruitmentId);
    }


    // 根据职位id加载不同状态的简历
    @GetMapping("/resume/data")
    public EmployerResult loadUnprocessedResume(HttpServletRequest request, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit, Integer id, String status,@RequestParam(defaultValue = "0") Integer getDay) {
        // 此时还可以从session中获取出id
        HttpSession session = request.getSession();
        // 加载招聘者信息
        Employer employer = (Employer) session.getAttribute("employer");
        Integer employerId = employer.getId();
        // 首先根据Integer id判断,如果用户没传入职位id,表示查询所有的简历
        if (null == id) {
            // 加载所有状 态简历的数量
//            List<ResumeAndDeliveryDTO> all = employerResumeService.loadResumeAll(employerId, status);
            List<ResumeAndDeliveryDTO> all = employerResumeService.loadResumeAllByTime(employerId,status,getDay);
            if (null == all || all.size() == 0) {
                return new EmployerResult("200", "无数据");
            }
            Integer count = all.size();
//            List<ResumeAndDeliveryDTO> resumeAndDeliveryDTOS = employerResumeService.loadResumeByPage(employerId, page, limit, status);
            List<ResumeAndDeliveryDTO> resumeAndDeliveryDTOS = employerResumeService.loadResumeByPageAndTime(employerId,page,limit,status,getDay);
            return new EmployerResult("200", "查询成功", resumeAndDeliveryDTOS, count);
        }
        // 走到这一步说明是根据职位id加载不同状态简历
        List<ResumeAndDeliveryDTO> all = employerResumeService.loadResumeByRecruitmentIdAllAndTime(employerId,status,getDay);
        if (null == all || all.size() == 0) {
            return new EmployerResult("200", "无数据");
        }
        Integer count = all.size();
        List<ResumeAndDeliveryDTO> resumeAndDeliveryDTOS = employerResumeService.loadResumeByRecruitmentIdPageAndTime(employerId,page,limit,status,getDay);
        return new EmployerResult("200", "查询成功", resumeAndDeliveryDTOS, count);
    }

    @PostMapping("/resume/update/status")
    public Result updateStatus(Integer id, String status) {
        int count = employerResumeService.updateResumeStatus(id, status);
        if (count == 0)
            return new Result("500", "抱歉,请重试");
        return new Result("200", "成功");
    }

    @PostMapping("/resume/delivery/delete")
    public Result DeliveryDelete(Integer id) {
        int count = employerResumeService.delResumeDelivery(id);
        if (count == 0)
            return new Result("500", "抱歉,请重试");
        return new Result("200", "删除成功");
    }

    @PostMapping("/resume/interview")
    public Result interview(Integer deliveryId, String interviewAddress, String interviewTime) {
        Date dateTime = DateUtil.parse(interviewTime, "yyyy-MM-dd HH:mm:ss");
        interviewService.saveToRabbit(deliveryId,interviewAddress,dateTime);
        return new Result("200", "成功");
    }


    @GetMapping("/interview/data")
    public EmployerResult interviewData(HttpServletRequest request, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit) {
        HttpSession session = request.getSession();
        // 加载招聘者信息
        Employer employer = (Employer) session.getAttribute("employer");
        Integer employerId = employer.getId();
        List<InterviewDTO> interviewDTOListAll = employerRecruitmentService.loadInterview(employerId);
        List<InterviewDTO> interviewDTOList = employerRecruitmentService.loadInterviewByPage(employerId, page, limit);
        return new EmployerResult("200", "查询成功", interviewDTOList, interviewDTOListAll.size());
    }

    @PostMapping("/interview/delete")
    public Result deleteInterview(Integer id){
        // 根据面试邀约id删除邀约
        interviewService.saveDelToRabbit(id);
        return new Result("200","删除成功");
    }

    @PostMapping("/interview/update")
    public Result updateInterview(Integer interviewId,String interviewAddress, String interviewTime){
        // 根据面试邀约id更新面试邀约时间和地点
//        System.out.println("邀约id："+interviewId);
//        System.out.println("地点："+interviewAddress);
//        System.out.println("时间："+interviewTime);
        Interview interview = interviewService.getById(interviewId);
        interview.setInterviewAddress(interviewAddress);
        interview.setInterviewTime(DateUtil.parse(interviewTime,"yyyy-MM-dd HH:mm:ss"));
        interviewService.saveUpdateToRabbit(interview);
        return new Result("200","成功");
    }


    @PostMapping("resume/view")
    public Result resumeView(HttpServletRequest request, Integer resumeId) {
        HttpSession session = request.getSession();
        // 加载招聘者信息
        Employer employer = (Employer) session.getAttribute("employer");
        Integer employerId = employer.getId();
        int count = employerResumeService.resumeView(resumeId, employerId, new Date());
        if (count == 2){
//            return "redirect:/resume_preview"
//            return null;
            return new Result("200", "成功");
        }
//            return new Result("200", "成功");
        return new Result("500", "失败");
    }


}
